﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;

namespace eKanbanWPF.View
{
    /// <summary>
    /// Interaction logic for TaskGUI.xaml
    /// </summary>
    public partial class TaskGUI : UserControl
    {
        private eKanbanWPF.Model.Task task = null;
        public event EventHandler Changed;

        public delegate void PositionChangedHandler(TaskGUI taskGui);
        public event PositionChangedHandler PositionChanged;

        void task_Changed(eKanbanWPF.Model.Task sender)
        {
            if (sender != null)
            {
                Width = sender.Width;
                Height = sender.Height;
                Canvas.SetLeft(this, sender.Left);
                Canvas.SetTop(this, sender.Top);
                ToolTip = task.Name;

                txtName.Text = task.Name;

                //«
                //txtPriority.Text = task.Priority.ToString();
                StringBuilder s = new StringBuilder();
                for (int i = 0; i < task.Priority; i++)
			    {
                    s.Append("«");
			    }
                txtPriority.Text = s.ToString();
                txtFrom.Text = task.StartTime.ToString("dd/MM/yy");
                txtDeadLine.Text = task.DeadLine.ToString("dd/MM/yy");
                txtDescription.Text = task.Description;

                // TODO: Add right type of image status to imgStatus
                //if (task.Status == Model.Task.StatusType.Ongoing)
                //{

                //}
                switch (task.Status)
                {
                    case eKanbanWPF.Model.Task.StatusType.Ongoing:
                        imgStatus.Source = statusOngoing.Source;
                        break;
                    case eKanbanWPF.Model.Task.StatusType.Completed:
                        imgStatus.Source = statusCompleted.Source;
                        break;
                    case eKanbanWPF.Model.Task.StatusType.Blocked:
                        imgStatus.Source = statusBlock.Source;
                        break;
                    case eKanbanWPF.Model.Task.StatusType.Defect:
                        imgStatus.Source = statusDefective.Source;
                        break;
                    default:
                        break;
                }
                //task.Name = txtTaskName;
                //task.StartTime =                 
            }
            else
            {
                ToolTip = "";
            }
            if (Changed != null)
            {
                Changed.Invoke(this, null);
            }
        }

        private void SetTask(eKanbanWPF.Model.Task value)
        {
            if (task != null)
            {
                task.Changed -= task_Changed;
            }
            task = value;
            if (task != null)
            {
                task.Changed += task_Changed;
            }
            task_Changed(value);
        }


        public eKanbanWPF.Model.Task Task
        {
            get { return task; }
            set
            {
                SetTask(value);
            }
        }

        public TaskGUI()
        {
            InitializeComponent();

            SetEvents();

            SetGuiProperties();
        }

        private void SetGuiProperties()
        {
            //grdContent.Background = this.Background;

        }

        private void SetEvents()
        {
            this.MouseDoubleClick += UserControl_MouseDoubleClick;
            this.MouseUp += UserControl_MouseUp;
            this.PreviewMouseDown += UserControl_PreviewMouseDown;
            this.LostFocus += UserControl_LostFocus;
        }

        private void UserControl_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            //phase.Width = Width;
            //phase.Height = Height;

            // only for testing GUI purpose
            if (task == null)
                Task = new Model.Task(new Model.Feature());

            task.Left = Canvas.GetLeft(this);
            task.Top = Canvas.GetTop(this);

            var r = FrmTask.Show(task);
            if (r == true)
            {
                task.Name = FrmTask.TaskName;
                task.Feature = FrmTask.Feature;
                task.StartTime = FrmTask.StartTime;
                task.FinishTime = FrmTask.FinishTime;
                task.DeadLine = FrmTask.Deadline;
                task.Description = FrmTask.Description;
                try
                {
                    task.Status = FrmTask.Status;
                }
                catch (Exception)
                {
                }
            }
        }

        private void UserControl_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            Selector.SetIsSelected((Control)sender, true);
            ((Control)sender).Focus();
        }

        private void UserControl_LostFocus(object sender, RoutedEventArgs e)
        {
            Selector.SetIsSelected((Control)sender, false);
            if (task != null)
            {
                bool ck = false;
                if (task.Left != Canvas.GetLeft(this) || task.Top != Canvas.GetTop(this))
                {
                    ck = true;
                }

                task.Width = Width;
                task.Height = Height;
                task.Left = Canvas.GetLeft(this);
                task.Top = Canvas.GetTop(this);
                if (Changed != null)
                {
                    Changed.Invoke(this, null);
                }

                if (ck)
                {
                    PositionChanged.Invoke(this);
                }
            }

        }

        private void UserControl_MouseUp(object sender, MouseButtonEventArgs e)
        {
            //MessageBox.Show(Canvas.GetLeft(this).ToString());
            if (task != null)
            {
                bool ck = false;
                if (task.Left != Canvas.GetLeft(this) || task.Top != Canvas.GetTop(this))
                {
                    ck = true;
                }

                task.Width = Width;
                task.Height = Height;
                task.Left = Canvas.GetLeft(this);
                task.Top = Canvas.GetTop(this);
                if (Changed != null)
                {
                    Changed.Invoke(this, null);
                }

                if (ck)
                {
                    PositionChanged.Invoke(this);
                }
            }
        }

    }
}
